package com.wushijia.highconcurrency.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class FutureTest1 {

  public static void main(String[] args) throws InterruptedException {
    CompletableFuture<Integer> completableFuture = new CompletableFuture<>();
    long start = System.currentTimeMillis();
    new Thread(new AskThread(completableFuture)).start();
    Thread.sleep(1000);
    completableFuture.complete(11);
    long end = System.currentTimeMillis();
    System.out.println("cost:" + (end - start));
  }

  public static class AskThread implements Runnable {

    private final CompletableFuture<Integer> completableFuture;

    public AskThread(CompletableFuture<Integer> completableFuture) {
      this.completableFuture = completableFuture;
    }

    @Override
    public void run() {
      int ret = 0;
      try {
        Thread.sleep(1000);
        ret = completableFuture.get() * completableFuture.get();
      } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
      }
      System.out.println(ret);
    }
  }

}
